<template>
  <div class="category">
    <header>
      <span>商品管理</span>
      <el-button type="primary" icon="el-icon-plus" class="add" @click="addCategory">添加分类</el-button>
    </header>
    <div class="content">
      <div class="title">当前商品id:{{categoryId}}</div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="品类id" width="100px"></el-table-column>
        <el-table-column prop="name" label="品类名称"></el-table-column>
        <el-table-column  label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="editCategoryName(scope.row)" type="text" size="small">编辑名称</el-button>
            
            <el-button @click="toChildCategory(scope.row)" type="text" size="small" v-show="categoryId==0">查看子品类</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { category,setCategoryName } from "../../util/netWork";

export default {
  data: () => ({
    tableData: [],
    categoryId:0
  }),
  mounted() {
    // 获取父级分类信息
    // this.categoryId=0
    this.getCategory(0);
  },
  methods: {
    // 添加分类
    addCategory() {
      this.$router.push("/addcategory")
    },
    // 获取分类数据
    getCategory(categoryId) {
      var data = {
        categoryId: categoryId
      };
      category(data).then(res => {
        // console.log(res)
        if (res.status == 0) {
          // console.log(res);
          this.tableData=res.data
        } else {
          alert(res.msg);
        }
      });
    },
    // 编辑品类名称
    editCategoryName(row) {
      // console.log(row);
      this.$prompt('请输入新的品类名称', 'happy mmall提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
          // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
          // inputErrorMessage: '邮箱格式不正确'
          inputValue:row.name
        }).then(({ value }) => {
          var data={
            categoryId:row.id,
            categoryName:value
          }
          //更改品类名称的请求
          setCategoryName(data).then(res=>{
            row.name=value
            this.$message({
            type: 'success',
            message: res.data+',新的品类名称是: ' + value
          });
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    // 查看子类
    toChildCategory(row) {
      this.categoryId=row.id;
      this.getCategory(this.categoryId)
    }
  },
  // activated(){
  //   this.categoryId=0;
  //   Object.assign(this.$data, this.$options.data())
  // }
};
</script>

<style lang="scss" scoped>
@import "@/assets/mixinScss.scss";
header {
  @include FontSize(30px);
  @include Size(100%, 100px);
  line-height: 100px;
  box-sizing: border-box;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .add {
    padding: 10px 5px;
  }
}


</style>